﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace AppCommon.Animation
{
    public class NotifyAnimationHandle
    {
        private NotifyIcon _NotifyIcon;
        public Icon[] Icons { get; set; }
        public Icon Success;
        public Icon Warning;
        public Icon Error;
        private int _ImageIndex = 0;
        private int _MaxAnimationTimes = -1;
        private int _CurrentAnimationTimes = 0;
        private bool _First = true;
        public string StartMessage { get; set; }
        public string StoppedMessage { get; set; }
        public string Title { get; set; }
        public double Interval
        {
            get { return objTimer.Interval; }
            set { objTimer.Interval = value; }
             }
        bool isStop = false;


        System.Timers.Timer objTimer;

        public NotifyAnimationHandle()
        {
            Init();
        }
        public NotifyAnimationHandle(NotifyIcon objNotity)
        {
            Init();
            _NotifyIcon = objNotity;
           
        }
        public void ShowError()
        {
            Stop();
            _NotifyIcon.Icon = Error;
        }
        public void ShowWarning()
        {
            Stop();
            _NotifyIcon.Icon = Warning;
        }
        public void ShowSuccess()
        {
            Stop();
            _NotifyIcon.Icon = Success;
        }
        
        public void Animation(int count)
        {
            isStop = false;
            if (_NotifyIcon == null || Interval <= 0)
                throw new Exception("Invaild parameter");

            Init();
            _MaxAnimationTimes = count;
            _CurrentAnimationTimes = 0;
            objTimer.Start();
        }
        public void Stop()
        {
            isStop = true;
            objTimer.Enabled = false;
        }
        private void Init()
        {
            if (objTimer == null)
            {
                objTimer = new System.Timers.Timer();
                objTimer.Enabled = false;
                objTimer.Elapsed += new System.Timers.ElapsedEventHandler(objTimer_Elapsed);
            }
            if (Icons == null)
            {
                string baseBath = Application.StartupPath + "\\Animation\\Notify\\";
                int i = 0;
                List<Icon> lst = new List<Icon>();
                while (true)
                {
                    if (File.Exists(baseBath + "Running" + i.ToString() + ".ico"))
                        lst.Add(Icon.ExtractAssociatedIcon(baseBath + "Running" + i.ToString() + ".ico"));
                    else
                        break;
                    i++;
                }
                Icons = lst.ToArray();
                Warning = Icon.ExtractAssociatedIcon(baseBath + "Warning.ico");
                Success = Icon.ExtractAssociatedIcon(baseBath + "Success.ico");
                Error = Icon.ExtractAssociatedIcon(baseBath + "Error.ico");
            }

        }

        void objTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (isStop)
                return;
            if (_First)
            {
                if (!String.IsNullOrEmpty(StartMessage))
                    _NotifyIcon.ShowBalloonTip(1000, Title, StartMessage, ToolTipIcon.None);
                _First = false;
            }
            if (_ImageIndex >= Icons.Length)
                _ImageIndex = 0;
            _NotifyIcon.Icon = Icons[_ImageIndex];
            _CurrentAnimationTimes = _MaxAnimationTimes > 0 ? _CurrentAnimationTimes++ : 0;
            if (_MaxAnimationTimes > 0 && _MaxAnimationTimes < _CurrentAnimationTimes)
                objTimer.Stop();
            _ImageIndex++;
        }
    }
}
